﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Types;
using GeoVision.Addin.Helpers;

namespace GeoVision.Addin.Core.Spatial
{
    public static class SQLGeoGenerator
    {

        public static void AddPolygonSegment(GeoShape dest, SqlGeometry g)
        {
            GeoShapeSegment segment = new GeoShapeSegment();
            segment.Type = GeometryType.Polygon;

            for (int i = 0; i < g.STNumPoints(); i++)
            {
                SqlGeometry p = g.STPointN(i + 1);
                GeoPoint pt = new GeoPoint(p.STY.Value, p.STX.Value);
                segment.Points.Add(pt);
            }

            dest.Segments.Add(segment);
        }

        public static GeoShape CreateShapes(SqlGeometry geo, double reduce)
        {
            GeoShape result = new GeoShape();
            geo = geo.MakeValid();

            for (int i = 0; i < geo.STNumGeometries(); i++)
            {

                SqlGeometry g = geo.STGeometryN(i + 1);
                if (reduce != 0.0)
                        g = g.Reduce(reduce);
                if (g.STIsEmpty())
                    continue;
                GeometryType type = GeometryHelper.GetGeometryType(g.STGeometryType().ToString().ToUpperInvariant());
                switch (type)
                {
                    case GeometryType.Polygon:
                        {
                            AddPolygonSegment(result, g);

                            break;
                        }
                    case GeometryType.MultiPolygon:
                        {
                            break;
                        }
                }
            }

            return result;
        }

        public static void AddPolygonSegment(GeoShape dest, SqlGeography g)
        {
            GeoShapeSegment segment = new GeoShapeSegment();
            segment.Type = GeometryType.Polygon;

            for (int i = 0; i < g.STNumPoints(); i++)
            {
                SqlGeography p = g.STPointN(i + 1);
                GeoPoint pt = new GeoPoint(p.Lat.Value, p.Long.Value);
                segment.Points.Add(pt);
            }

            dest.Segments.Add(segment);
        }

        public static GeoShape CreateShapes(SqlGeography geo, double reduce)
        {
            GeoShape result = new GeoShape();

            for (int i = 0; i < geo.STNumGeometries(); i++)
            {

                SqlGeography g = geo.STGeometryN(i + 1);

                if (reduce != 0.0)
                    g = g.Reduce(reduce);

                if (g.STIsEmpty())
                    continue;
                GeometryType type = GeometryHelper.GetGeometryType(g.STGeometryType().ToString().ToUpperInvariant());
                switch (type)
                {
                    case GeometryType.Polygon:
                        {
                            AddPolygonSegment(result, g);

                            break;
                        }
                    case GeometryType.MultiPolygon:
                        {
                            break;
                        }
                }
            }

            return result;
        }
    }
}
